<template>
  <!-- home页面容器 -->
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <!-- 左侧logo和字体 -->
      <div>
        <img src="../assets/heima.png" alt="" />
        <span>电商后台管理系统</span>
      </div>
      <!-- 右侧退出登录按钮 -->
      <el-button type="primary" @click="loginout">退出</el-button>
    </el-header>
    <el-container>
      <!-- 侧边栏区域 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <!-- 折叠功能区域 -->
        <div @click="toggleCollapse" class="toggleCollapse">|||</div>
        <!-- 侧边栏菜单区域 -->
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="rgb(70,156,248)"
          :unique-opened="false /*只能单个打开*/"
          :collapse="isCollapse /*折叠情况*/"
          :collapse-transition="false /*是否开启折叠动画过渡*/"
          :router="true /*开启路由功能，index就是路径*/"
        >
          <!-- 一级菜单 -->
          <el-submenu
            :key="item.id"
            v-for="(item, index) in menulist"
            :index="item.id.toString()"
          >
            <template slot="title">
              <i :class="menuIconArr[index]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :key="item2.id"
              v-for="item2 in item.children"
              :index="'/' + item2.path"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ item2.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 页面主体区域 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  created() {
    //methods和data已经初始化,常用于操作数据，发起ajax请求
    //获取菜单数据
    this.getMenuList();
  },
  data() {
    return {
      //存储菜单列表的数组
      menulist: [],
      //菜单存放小图标的对象
      menuIconArr: [
        "el-icon-s-custom",
        "el-icon-s-platform",
        "el-icon-s-cooperation",
        "el-icon-s-order",
        "el-icon-bank-card",
      ],
      //判断菜单栏是否折叠的变量
      isCollapse: false,
    };
  },
  methods: {
    //退出登录功能
    loginout() {
      //删除token
      sessionStorage.removeItem("token");
      //跳转到登录页面
      this.$router.push("/login");
    },
    //获取菜单列表
    async getMenuList() {
      //发送请求 并保存数据
      var { data: ret } = await this.$http.get(`menus`);
      // console.log(ret);
      this.menulist = ret.data;
    },
    //折叠导航栏功能
    toggleCollapse() {
      //将菜单栏的折叠状态取反
      this.isCollapse = !this.isCollapse;
    },
  },
};
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  padding-left: 10px;
  display: flex;
  background-color: rgb(55, 61, 65);
  justify-content: space-between;
  align-items: center;
  div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
      font-size: 20px;
      color: #ccc;
    }
  }
}
.el-aside {
  background-color: rgb(51, 55, 68);
}
.el-main {
  background-color: #eaedf1;
}
.el-menu {
  border-right: none;
}
.toggleCollapse {
  color: rgb(144, 147, 153);
  text-align: center;
  font-size: 12px;
  cursor: pointer;
  letter-spacing: 0.15rem;
  padding: 7px 0;
  &:hover {
    background-color: rgb(41, 44, 54);
  }
}
</style>